package com.put.mybills.adapter;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;
import com.put.mybills.database.BillDataSource;
import com.put.mybills.database.FactureDataSource;
import com.put.mybills.database.SQLLite2XML;
import com.put.mybills.database.TemplateBarcodeDataSource;
import com.put.mybills.database.UnitDataSource;

public class GlobalParams {
	
	private static final String TAG = "GlobalParams";
	
    private static GlobalParams instance = null;
    
    public int barcodeStartIndex = 5;
    public int barcodeEndIndex=10;
    
    public String pathSDcard = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator ;
    public String pathToExternalDir = "";
    
    public SQLLite2XML parser;
   
    public FactureDataSource facturedatasource;
    public BillDataSource billdatasource;
    public UnitDataSource unitdatasource;
    public TemplateBarcodeDataSource templatedatasource;
    
    public String app_fr_counter = "app_counter";
    
    public String app_folder_name = "MyFacture";
    public String app_folder_name_sp = "app_dir";
    
    public String facture_icon_dir = "icons";
    public String facture_icon_dir_sp = "icon_dir";
    
    public String bill_picture_dir = "bills";
    public String bill_picture_dir_sp = "bills_sp";
    
    
    public String app_data_dir = "data";
    public String app_data_dir_sp = "data_sp";    
    public String facture_icon_prefix = "icon_";    

    private Activity context;
    
    public GlobalParams(Activity context) { 
    	
       instance = this;
	   this.context = context;
	   
	   readSettings();
	   
	   Log.d(TAG, "GlobalParams constructor");
	  
	   //context.deleteDatabase("factureManager");
	  
	   facturedatasource = new FactureDataSource(context);
	   facturedatasource.open(); 
	   
	   facturedatasource.createDataRows();
	   
	   billdatasource = new BillDataSource(context);
	   billdatasource.open();
	   
	   unitdatasource = new UnitDataSource(context);
	   unitdatasource.open();
	   
	   templatedatasource = new TemplateBarcodeDataSource(context);
	   templatedatasource.open();
	   
	   parser = new SQLLite2XML();	   
   
    }
    
    
    public GlobalParams(Context context) { 
    	
       instance = this;
 	   //this.context = context;
 	   
 	   //readSettings();
 	   
 	   Log.d(TAG, "GlobalParams constructor");
 	  
 	   //context.deleteDatabase("factureManager");
 	  
 	   facturedatasource = new FactureDataSource(context);
 	   facturedatasource.open(); 
 	   
 	   facturedatasource.createDataRows();
 	   
 	   billdatasource = new BillDataSource(context);
 	   billdatasource.open();
 	   
 	   unitdatasource = new UnitDataSource(context);
 	   unitdatasource.open();
 	   
 	   parser = new SQLLite2XML();	   
    
     }
   
   protected GlobalParams(){}
   
   public static GlobalParams getInstance() {
      if(instance == null) {
         instance = new GlobalParams();
      }
      return instance;
   }
   
   
   public Boolean readSettings() {
		
		Log.d(TAG,"Read settings");
		
		SharedPreferences sharedPref = context.getPreferences(Context.MODE_PRIVATE);   
		GlobalParams.getInstance().pathToExternalDir = GlobalParams.getInstance().pathSDcard+  sharedPref.getString("app_dir_name", "MyFacture") + File.separator ;
		Log.d(TAG, " Path to external dir " + GlobalParams.getInstance().pathToExternalDir );
		File factureDirectory = new File(GlobalParams.getInstance().pathToExternalDir);
		factureDirectory.mkdirs();
		
		
		return true;
	}
	
   
  

}
